{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-01-08T11:53:31.468220Z",
     "start_time": "2025-01-08T11:53:30.636853Z"
    }
   },
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt"
   ],
   "outputs": [],
   "execution_count": 1
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "",
   "id": "aa467d5629a75ce2"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 1、练习groupby\n",
   "id": "c3c4c273f2970b74"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T12:15:39.791315Z",
     "start_time": "2025-01-08T12:15:39.783452Z"
    }
   },
   "cell_type": "code",
   "source": [
    "dict_obj = {'name': ['Alice', 'Bob', 'Charlie', 'David', 'Eve'], \n",
    "            'age': [25, 30, 35, 40, 45], \n",
    "            'gender': ['F', 'M', 'F', 'M', 'F']}\n",
    "df = pd.DataFrame(dict_obj)\n",
    "print(df)"
   ],
   "id": "5e5f5e9009cdb95",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "      name  age gender\n",
      "0    Alice   25      F\n",
      "1      Bob   30      M\n",
      "2  Charlie   35      F\n",
      "3    David   40      M\n",
      "4      Eve   45      F\n"
     ]
    }
   ],
   "execution_count": 3
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T12:16:08.523182Z",
     "start_time": "2025-01-08T12:16:08.517573Z"
    }
   },
   "cell_type": "code",
   "source": [
    "sum = df.groupby('gender')['age'].sum()\n",
    "print(sum)"
   ],
   "id": "8ad820de382e403f",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "gender\n",
      "F    105\n",
      "M     70\n",
      "Name: age, dtype: int64\n"
     ]
    }
   ],
   "execution_count": 4
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T12:28:18.387205Z",
     "start_time": "2025-01-08T12:28:18.380605Z"
    }
   },
   "cell_type": "code",
   "source": [
    "df['gender_age'] = df.groupby('gender')['age'].transform(lambda x: x.sum())\n",
    "print(df)"
   ],
   "id": "814b34e7c70da4f3",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "      name  age gender  gender_age\n",
      "0    Alice   25      F         105\n",
      "1      Bob   30      M          70\n",
      "2  Charlie   35      F         105\n",
      "3    David   40      M          70\n",
      "4      Eve   45      F         105\n"
     ]
    }
   ],
   "execution_count": 11
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 2、练习merge",
   "id": "76892eddd41eddcf"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T12:31:43.915134Z",
     "start_time": "2025-01-08T12:31:43.907381Z"
    }
   },
   "cell_type": "code",
   "source": [
    "df1 = pd.DataFrame({'A': ['A0', 'A1', 'A2', 'A3'],\n",
    "                    'B': ['B0', 'B1', 'B2', 'B3'],\n",
    "                    'C': ['C0', 'C1', 'C2', 'C3']},\n",
    "                   index=[0, 1, 2, 3])\n",
    "\n",
    "df2 = pd.DataFrame({'A': ['A0', 'A1', 'A2', 'A3'],  \n",
    "                    'B': ['B0', 'B1', 'B2', 'B3'],\n",
    "                    'D': ['D0', 'D1', 'D2', 'D3']},\n",
    "                   index=[4, 5, 6, 7])\n",
    "\n",
    "df3 = pd.merge(df1, df2, on=['A', 'B']) ##使用merge()根据A和B列进行合并\n",
    "print(df3)"
   ],
   "id": "882e2590e0771dd7",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    A   B   C   D\n",
      "0  A0  B0  C0  D0\n",
      "1  A1  B1  C1  D1\n",
      "2  A2  B2  C2  D2\n",
      "3  A3  B3  C3  D3\n"
     ]
    }
   ],
   "execution_count": 12
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T12:33:18.593503Z",
     "start_time": "2025-01-08T12:33:18.587255Z"
    }
   },
   "cell_type": "code",
   "source": [
    "df4 = pd.merge(df1, df2, on=['A', 'B'], how='outer') ##使用merge()根据A和B列进行合并，how参数指定合并方式，outer表示合并所有行和列\n",
    "print(df4)"
   ],
   "id": "67ea49d9d06f5790",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    A   B   C   D\n",
      "0  A0  B0  C0  D0\n",
      "1  A1  B1  C1  D1\n",
      "2  A2  B2  C2  D2\n",
      "3  A3  B3  C3  D3\n"
     ]
    }
   ],
   "execution_count": 13
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 3、练习重复值处理",
   "id": "1a27f94c6016f87"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T12:37:34.800376Z",
     "start_time": "2025-01-08T12:37:34.794028Z"
    }
   },
   "cell_type": "code",
   "source": [
    "data = {\n",
    "    'Name' : ['Alice', 'Bob', 'Charlie', 'David', 'Eve', 'Alice', 'Bob', 'Charlie', 'David', 'Eve'],\n",
    "    'Age' : [25, 30, 35, 40, 45, 25, 30, 35, 40, 45]\n",
    "}\n",
    "df = pd.DataFrame(data)\n",
    "print(df)\n",
    "print(df.duplicated())"
   ],
   "id": "a3f2b02ae4eeb180",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "      Name  Age\n",
      "0    Alice   25\n",
      "1      Bob   30\n",
      "2  Charlie   35\n",
      "3    David   40\n",
      "4      Eve   45\n",
      "5    Alice   25\n",
      "6      Bob   30\n",
      "7  Charlie   35\n",
      "8    David   40\n",
      "9      Eve   45\n",
      "0    False\n",
      "1    False\n",
      "2    False\n",
      "3    False\n",
      "4    False\n",
      "5     True\n",
      "6     True\n",
      "7     True\n",
      "8     True\n",
      "9     True\n",
      "dtype: bool\n"
     ]
    }
   ],
   "execution_count": 17
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T12:36:35.402710Z",
     "start_time": "2025-01-08T12:36:35.397588Z"
    }
   },
   "cell_type": "code",
   "source": [
    "df.drop_duplicates(inplace=True)\n",
    "print(df)"
   ],
   "id": "bac9ff8122b6b275",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "      Name  Age\n",
      "0    Alice   25\n",
      "1      Bob   30\n",
      "2  Charlie   35\n",
      "3    David   40\n",
      "4      Eve   45\n"
     ]
    }
   ],
   "execution_count": 15
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T12:38:35.331922Z",
     "start_time": "2025-01-08T12:38:35.326419Z"
    }
   },
   "cell_type": "code",
   "source": "print(df.drop_duplicates(subset=['Name'])) ##按照Name列进行去重",
   "id": "6c9cb03816ac3752",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "      Name  Age\n",
      "0    Alice   25\n",
      "1      Bob   30\n",
      "2  Charlie   35\n",
      "3    David   40\n",
      "4      Eve   45\n"
     ]
    }
   ],
   "execution_count": 18
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T12:40:44.680351Z",
     "start_time": "2025-01-08T12:40:44.675450Z"
    }
   },
   "cell_type": "code",
   "source": [
    "df.replace(to_replace='Alice', value='Alicia', inplace=True)\n",
    "print(df)"
   ],
   "id": "390acb7da974ac76",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "      Name  Age\n",
      "0   Alicia   25\n",
      "1      Bob   30\n",
      "2  Charlie   35\n",
      "3    David   40\n",
      "4      Eve   45\n",
      "5   Alicia   25\n",
      "6      Bob   30\n",
      "7  Charlie   35\n",
      "8    David   40\n",
      "9      Eve   45\n"
     ]
    }
   ],
   "execution_count": 19
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "",
   "id": "58c82442bf28f08c"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
